package com.blog.effective.note7;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * 测试避免使用终结方法.
 *
 * @author 张俊强~.
 * @date 2017/11/27-21:49.
 */
public class FinalizerMethod {
    public static void main(String[] args) {

        int i = 10000;
        while (i-- != 0) {
            FileInputStream inputStream = null;
            //读取文件内容并输出
            try {
                inputStream = new FileInputStream(new File("F:\\logs\\test.txt"));

                byte[] bytes = new byte[1024];
                StringBuffer buffer = new StringBuffer();

                int len;
                while ((len = inputStream.read(bytes)) > 0) {//将文件流的内容写入到相应的字节数组中去
                    buffer.append(new String(bytes, 0, len, "UTF-8"));
                }
                System.out.println(buffer);

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {

                e.printStackTrace();
            } finally {
                System.out.println("文件的IO流被释放");
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
